﻿using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using GalaSoft.MvvmLight.Messaging;
using LiSAClient.Core.Messages;
using LiSAClient.WpfClient.Utils;
using LiSAClient.WpfClient.ViewModel.Frame.Menus;
using LiSAClient.WpfClient.ViewModel.Projects;
using LiSAClient.WpfClient.ViewModel.Users;

namespace LiSAClient.WpfClient.ViewModel.Frame.Windows
{
    internal class MainWindowViewModel : BasePresenter, IMenuHandler
    {
        #region Observable objects
        public string Version
        {
            get
            {
                return string.Format("LiSA ver. {0}", 0.2);
            }
        }

        private MenuViewModel mainMenu;

        public MenuViewModel MainMenu
        {
            get
            {
                return this.mainMenu;
            }

            set
            {
                this.Set(() => this.MainMenu, ref this.mainMenu, value);
            }
        }

        private MenuViewModel leftMenu;

        public MenuViewModel LeftMenu
        {
            get
            {
                return this.leftMenu;
            }

            set
            {
                this.Set(() => this.LeftMenu, ref this.leftMenu, value);
            }
        }

        private bool showMenues;

        public bool ShowMenues
        {
            get
            {
                return this.showMenues;
            }

            private set
            {
                this.showMenues = value;
                this.RaisePropertyChanged("ShowMenues");
            }
        }

        private UserInformationFields userInfo;

        public UserInformationFields UserInfo
        {
            get
            {
                return this.userInfo;
            }

            set
            {
                this.Set(() => this.UserInfo, ref this.userInfo, value);
            }
        }
        #endregion

        #region Initializers
        internal MainWindowViewModel()
        {
            this.MainMenu = new MenuViewModel();
            this.LeftMenu = new MenuViewModel();
            this.showMenues = false;
            this.ShowLogin();
            Messenger.Default.Register<LoginCompletedMessage>(this, this.LoginCompleted);
        }
        #endregion

        #region Methods
        private void LoginCompleted(LoginCompletedMessage obj)
        {
            this.UserInfo = new UserInformationFields { CurrentUser = obj.LoggedInUser };
            this.ShowDefault();
        }

        private void ShowLogin()
        {
            var loginViewModel = new LoginViewModel();
            loginViewModel.SetPresenter(this);
            this.PresentContent(loginViewModel);
        }

        private void ShowDefault()
        {
            this.StartWaiting("Fetching menus...");
            Task.Factory.StartNew(() =>
            {
                this.ShowMenues = true;
                AssemblyLoader.Load();
                this.BuildMenu();
            });
        }

        private void BuildMenu()
        {
            var fullMenu = this.BuildFullMenu();
            this.FinishedWaiting();
            this.MainMenu.Fill(fullMenu, fullMenu.First().Children.First());
        }

        private List<MenuItemBase> BuildFullMenu()
        {
            var projectOverview = new ProjectsOverviewViewModel();

            var list = new List<MenuItemBase>();

            list.Add(new MainMenuItem("Front Page", this)
                        .Add(new HiddenMenuItem(this, () => new FrontPageViewModel())));

            list.Add(new MainMenuItem("Projects", this)
                .Add(new LeftMenuItem("Projects Overview", "pack://application:,,,/LiSAClient.WpfClient;component/Resources/Media/Images/ButtonIcons/list.png", () => new ProjectsOverviewViewModel {ShowInWindow = false}, this))
                .Add(new LeftMenuItem("New Project", "pack://application:,,,/LiSAClient.WpfClient;component/Resources/Media/Images/ButtonIcons/plus.png", () => new NewProject_Step1ViewModel { ShowInWindow = true }, this, false)));
           
            return list;
        }

        public void ActivateMainMenuItem(MainMenuItem menuItem)
        {
            if (menuItem != null && menuItem.Children.Count > 0)
            {
                this.LeftMenu.Fill(menuItem.Children.ToList(), menuItem.Children.First());
            }
        }

        public void ActivateLeftMenuItem(LeftMenuItem menuItem)
        {
            if (menuItem != null && menuItem.Content != null)
            {
                // Creates a new instance of the content.
                var content = menuItem.Content();
                this.PresentContent(content);
            }
        }

        public void ActivateHiddenMenuItem(HiddenMenuItem menuItem)
        {
            if (menuItem != null && menuItem.Content != null)
            {
                // Creates a new instance of the content.
                var content = menuItem.Content();
                this.PresentContent(content);
            }
        }
        #endregion
    }
}
